//
//  HUD+Square.swift
//  BWHUDKit
//
//  Created by zhuxuhong on 2022/3/21.
//

import UIKit
import BWToolKit

extension HUD {
    public static func square(
        customView: UIView,
        duration: TimeInterval = Duration.short
    ) {
        show(.square(.init(text: nil, customView: customView, type: .custom, duration: duration)))
    }
    public static func progress(
        _ type: Square.ProgressType,
        value: CGFloat,
        text: String? = nil,
        canceller: BlockVoid? = nil,
        completion: BlockVoid? = nil
    ) {
        guard
            let vc = showings.first(where: {
                HUDType.progress.isMatched(of: $0)
            }) as? HUDSquareVC
        else {
            show(.square(.init(text: text, customView: nil, type: .progress(type: type), progress: value, canceller: canceller)), completion: completion)
            return
        }
        vc.updateProgress(value, text: text)
    }
    public static func loading(
        _ type: Square.LoadingType = .indicator,
        text: String? = nil,
        duration: TimeInterval = .infinity
    ) {
        show(.square(.init(text: text, customView: nil, type: .loading(type: type), duration: duration)))
    }
    public static func tips(
        _ type: Square.TipsType,
        text: String? = nil
    ) {
        show(.square(.init(text: text, customView: nil, type: .tips(type: type))))
    }
}


extension HUD {
    public class Square: Data {
        var type: ShowType
        var progress: CGFloat
        var canceller: BlockVoid?
        
        public init(
            text: String? = nil,
            customView: UIView? = nil,
            type: ShowType = .custom,
            duration: TimeInterval = .infinity,
            progress: CGFloat = 0,
            maskColor: UIColor = .clear,
            canceller: BlockVoid? = nil
        ) {
            self.canceller = canceller
            self.progress = progress
            var d = duration
            if case .tips = type {
                d = Duration.fit(text: text)
            }
            var showType = type
            var showView: UIView? = nil
            if let _ = customView {
                showType = .custom
            }
            if case .custom = showType, let view = customView {
                showView = view
            }
            self.type = showType
            super.init(icon: nil, text: text, customView: showView, duration: d, maskColor: maskColor, blockable: true, position: .center)
        }
    }
}

extension HUD.Square {
    public enum ShowType {
        case custom
        case loading(type: LoadingType)
        case progress(type: ProgressType)
        case tips(type: TipsType)
    }
    public enum LoadingType {
        case indicator, ring
        case custom(_ images: [UIImage])
    }
    public enum ProgressType {
        case pie, ring, slider, water
    }
    
    public enum TipsType {
        case success, error, warning
        case custom(_ image: UIImage?)
        
        static let lineWidth = CGFloat(3)
        
        static let iconCheckmark = UIImage.checkmark(iconSize: .init(width: 60, height: 30), imageSize: .bw_with(both: 60), lineWidth: Self.lineWidth)
        static let iconXmark = UIImage.xmark(iconSize: .bw_with(both: 40), imageSize: .bw_with(both: 60), lineWidth: Self.lineWidth)
        static let iconExcmark = UIImage.excmark(iconSize: .bw_with(both: 40), imageSize: .bw_with(both: 60), lineWidth: Self.lineWidth)
        
        var icon: UIImage? {
            switch self {
            case .success:
                return Self.iconCheckmark
            case .error:
                return Self.iconXmark
            case .warning:
                return Self.iconExcmark
            case .custom(let image):
                return image
            }
        }
    }
}
